package com.infocollect.project.api.service.impl;


import com.alibaba.fastjson.JSON;
import com.infocollect.project.api.entity.TappFromEntity;
import com.infocollect.project.api.enums.ApiTypeEnum;
import com.infocollect.project.api.req.*;

import java.util.*;
import java.util.stream.Collectors;


import com.infocollect.project.api.service.ITappApiConfigService;
import com.infocollect.project.api.service.ITappFromService;
import com.infocollect.project.api.vo.ColumnInfoVO;
import com.infocollect.project.api.vo.ExportDetailSqlVO;
import com.infocollect.project.api.vo.QuestionnaireSqlContentVO;
import com.infocollect.project.api.vo.TableChildInfoVO;
import com.infocollect.project.form.vo.HeadEntityVO;
import com.infocollect.project.form.vo.ParamBizFromVO;
import com.infocollect.project.form.vo.R;
import com.infocollect.project.api.req.*;
import net.sf.json.JSONObject;
import ognl.Ognl;
import ognl.OgnlContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class QuestionnaireServiceImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 服务对象
     */
    @Resource
    private ITappApiConfigService tappApiconfigService;
    @Resource
    private ITappFromService tappFromService;
    /**
     * 服务对象
     */
    @Resource
    private TappPageServiceImpl pageService;
    JSONObject param = null;
    OgnlContext context;
    String fromname;
    /***
     * 更新表结构
     * @param fromdesignjson 设计表单json
     * @param tableName
     */
    public void TableEidt(String fromdesignjson, String tableName) {
        param = null;
        context = null;
        fromname="";
        List<LayoutModelReq> layoutModelReqList = GetFormControl(fromdesignjson, "layoutitemlist[0].widgetForm.list", LayoutModelReq.class);
        fromname=param.getString("formName");
        if (layoutModelReqList == null)
            return;
        final int rowCount = jdbcTemplate.queryForObject("SELECT count(*) totalcount FROM information_schema.TABLES WHERE table_schema =(SELECT DATABASE ()) and table_name='" + tableName + "'", Integer.class);
        if (rowCount == 0) {
            CreateTable(layoutModelReqList, tableName);
        } else {
            AlterTable(layoutModelReqList, tableName);
        }
    }

    ///根据提交设计json  获取设计的json
    List<FormControlReq> GetFormControl(String fromdesignjson, String jsonPath) {
        return GetFormControl(fromdesignjson,jsonPath, FormControlReq.class);
    }
    <T> List<T> GetFormControl(String jsonPath, Class<T> clazz){
        return GetFormControl("",jsonPath, clazz);
    }
    <T> List<T> GetFormControl(String fromdesignjson,String jsonPath, Class<T> clazz) {
        try {
            if (param == null) {
                param = JSONObject.fromObject(fromdesignjson);
                context = getOgnlContext(param);
            }
            Object expression = Ognl.parseExpression(jsonPath);
            Object value = Ognl.getValue(expression, context, context);
            return JSON.parseArray(JSON.toJSONString(value), clazz);
            ///   return  JSONObject.jsonToObjList(FormControlReq.class,JSONUtil.objToJson(value));
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }
    }

    private OgnlContext getOgnlContext(JSONObject param) {
        OgnlContext context = new OgnlContext();
        Iterator<String> it = param.keys();
        while (it.hasNext()) {
            String key = it.next();  // 获得key
            Object value = param.get(key);
            context.put(key, value);
        }
        return context;
    }

    ///新增 创建表
    public void CreateTable(List<LayoutModelReq> layoutModelReqList, String tableName) {
        String createTableSqlstr="CREATE TABLE `"+tableName+"`  (\n" +
                "  `fid` varchar(20) NOT NULL ,\n" +
                "  `fcreationdate` datetime(0) DEFAULT NULL,\n" +
                "  `fcreateby` varchar(20)   DEFAULT NULL,\n" +
                "  `fcreatebyname` varchar(100)   DEFAULT NULL,\n" +
                "  `flastupdatedate` datetime(0) DEFAULT NULL,\n" +
                "  `flastupdateby` varchar(20)   DEFAULT NULL,\n"+
                "  `flastupdatebyname` varchar(100)   DEFAULT NULL,\n"+
                "  `auditdate` datetime(0) DEFAULT NULL,\n" +
                "  `auditby` varchar(20)   DEFAULT NULL,\n"+
                "  `auditbyname` varchar(100)   DEFAULT NULL,\n"+
                "  `auditflag` varchar(20)   DEFAULT '未审核',\n"+
                "  `openid` varchar(50)   DEFAULT null,\n"+
                "  `isimport` varchar(10)   DEFAULT '否',\n"+
                "  `auditremark` varchar(200)   DEFAULT null,\n";
        StringBuffer createTableSql= new StringBuffer();
        StringBuffer selectListSql = new StringBuffer();
        StringBuffer updatesql = new StringBuffer();
        StringBuffer saveInfoSql = new StringBuffer();
        updatesql.append("update `"+tableName+"` set auditbyname=#{$user.userName}, auditby=#{$user.userId},auditdate=now(),flastupdatebyname=#{$user.userName}, flastupdateby=#{$user.userId},flastupdatedate=now(), openid=#{openid}, auditflag=#{auditflag}, isimport=#{isimport}, auditremark=#{auditremark} ");
        StringBuffer insertfiled = new StringBuffer();
        StringBuffer insertfiledValue = new StringBuffer();
        StringBuffer insertChildTableSql = new StringBuffer();
        selectListSql.append("select maintable.* from `"+tableName+"` as maintable <where> ");
        List<HeadEntityVO> headEntityVOS=new ArrayList<>();
        Map<String,ExportDetailSqlVO> exportDetailSql=new HashMap<>();
        StringBuilder whereSqlBuilder=new StringBuilder();
        StringBuilder chartQuerySql=new StringBuilder();
        List<ParamBizFromVO> paramlist=new ArrayList<>();
        for(LayoutModelReq layoutModelReq:layoutModelReqList) {
            switch (layoutModelReq.getType()){
                case "gridPanel":
                    for (FormControlReq req:layoutModelReq.getColumns().get(0).getList()) {
                        List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist=getQuestionnaireSqlContent(req, paramlist,headEntityVOS);
                        for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist ) {
                            if (questionnaireSqlContentVO != null) {
                                if (!req.type.equals("listview")) {
                                    insertfiled.append("," +questionnaireSqlContentVO.getFiled());
                                    createTableSql.append(questionnaireSqlContentVO.getTableEidtSql() + ",");
                                    whereSqlBuilder.append(questionnaireSqlContentVO.getSelectListSql());
                                    updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                                    insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                        if ("".equals(chartQuerySql.toString())) {
                                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                        } else {
                                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                        }
                                    }
                                } else {
                                    saveInfoSql.append(questionnaireSqlContentVO.getSaveInfoSql());
                                    insertChildTableSql.append(questionnaireSqlContentVO.getInsertChildTableSql());
                                    exportDetailSql.put(req.options.controlId, new ExportDetailSqlVO());
                                    exportDetailSql.get(req.options.controlId).setExportDetailSql("select detailtable.* from " + req.options.controlId + " as detailtable inner join `" + tableName + "` as maintable on detailtable.fparentid=maintable.fid  <where> <if test=\"fcreateby!=null and fcreateby!=''\">and maintable.fcreateby=#{fcreateby} </if>");
                                    exportDetailSql.get(req.options.controlId).setHeadEntity(questionnaireSqlContentVO.getHeadEntity());
                                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                        if ("".equals(chartQuerySql.toString())) {
                                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                        } else {
                                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case "collapse":
                case "tabs":
                    for (LayoutModelPanelistReq layoutModelPanelistReq:layoutModelReq.getOptions().getPanelist()){
                        for (FormControlReq req:layoutModelPanelistReq.getColumns().get(0).getList()) {
                            List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist= getQuestionnaireSqlContent(req,paramlist,headEntityVOS);
                            for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist ) {
                                if (questionnaireSqlContentVO != null) {
                                    if (!req.type.equals("listview")) {
                                        insertfiled.append("," +questionnaireSqlContentVO.getFiled());
                                        createTableSql.append(questionnaireSqlContentVO.getTableEidtSql() + ",");
                                        whereSqlBuilder.append(questionnaireSqlContentVO.getSelectListSql());
                                        updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                                        insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                                        if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                            if ("".equals(chartQuerySql.toString())) {
                                                chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                            } else {
                                                chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                            }
                                        }
                                    } else {
                                        saveInfoSql.append(questionnaireSqlContentVO.getSaveInfoSql());
                                        insertChildTableSql.append(questionnaireSqlContentVO.getInsertChildTableSql());
                                        exportDetailSql.put(req.options.controlId, new ExportDetailSqlVO());
                                        exportDetailSql.get(req.options.controlId).setExportDetailSql("select detailtable.* from " + req.options.controlId + " as detailtable inner join `" + tableName + "` as maintable on detailtable.fparentid=maintable.fid  <where> <if test=\"fcreateby!=null and fcreateby!=''\">and maintable.fcreateby=#{fcreateby} </if>");
                                        exportDetailSql.get(req.options.controlId).setHeadEntity(questionnaireSqlContentVO.getHeadEntity());
                                        if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                            if ("".equals(chartQuerySql.toString())) {
                                                chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                            } else {
                                                chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                default:break;
            }
        }
        GetDefaultHeadEntity(headEntityVOS,paramlist);
        whereSqlBuilder.append("<if test=\"fcreationdate!=null and fcreationdate.size()>0\">and maintable.fcreationdate between #{fcreationdate[0]} and #{fcreationdate[1]}  </if>");
        whereSqlBuilder.append("<if test=\"fcreatebyname!=null and fcreatebyname!=''\">and maintable.fcreatebyname  like CONCAT('%',#{fcreatebyname},'%')  </if>");
        whereSqlBuilder.append("<if test=\"auditbyname!=null and auditbyname!=''\">and maintable.auditbyname  like CONCAT('%',#{auditbyname},'%')  </if>");
        whereSqlBuilder.append("<if test=\"auditflag!=null and auditflag!=''\">and maintable.auditflag =#{auditflag}  </if>");
        whereSqlBuilder.append("<if test=\"openid!=null and openid!=''\">and maintable.openid  = #{openid}  </if>");
        whereSqlBuilder.append("<if test=\"isimport!=null and isimport!=''\">and maintable.isimport  = #{isimport}  </if>");
        whereSqlBuilder.append("<if test=\"auditremark!=null and auditremark!=''\">and maintable.auditremark  = #{auditremark}  </if>");
        selectListSql.append(whereSqlBuilder.toString());
        selectListSql.append("</where>");
        createTableSqlstr=createTableSqlstr+  createTableSql.toString()+"   PRIMARY KEY (`fid`)) comment='"+fromname+"';";
        String selectSql="select *from "+tableName+" where fid=#{fid}";
        updatesql.append(" where fid=#{$tableNewId}");
        String insertSql="insert into "+tableName+"(fid,fcreationdate,fcreateby,fcreatebyname,flastupdatedate,flastupdateby,flastupdatebyname,openid,auditremark "+insertfiled.toString()+") values(#{$tableNewId},now(),#{$user.userId},#{$user.userName},now(),#{$user.userId},#{$userName},#{openid},#{auditremark} "+insertfiledValue.toString()+")";
        jdbcTemplate.execute(createTableSqlstr);
        R<String> r= pageService.addPage(tableName,fromname);
        SaveApiConfigReq apiParam=new SaveApiConfigReq();
        if(r.getCode()==0) {
            apiParam.setMethodType(ApiTypeEnum.LIST);
            apiParam.setMethodName("查询列表");
            apiParam.setMethodCode("querylist");
            List<ParamBizFromVO> rootbizFromlist=new ArrayList<>();
            ParamBizFromVO bizFromVO=new ParamBizFromVO();
            bizFromVO.setParamname("Post参数");
            bizFromVO.setVariabletype("Object");
            bizFromVO.setIsfirst(true);
            bizFromVO.setProperty("body");
            bizFromVO.setChildren(paramlist);
            rootbizFromlist.add(bizFromVO);
            ParamBizFromVO bizFromVO1=new ParamBizFromVO();
            bizFromVO1.setParamname("URL参数");
            bizFromVO1.setVariabletype("Object");
            bizFromVO1.setIsfirst(true);
            bizFromVO1.setProperty("query");
            rootbizFromlist.add(bizFromVO1);
            ParamBizFromVO bizFromVO2=new ParamBizFromVO();
            bizFromVO2.setParamname("header头部参数");
            bizFromVO2.setVariabletype("Object");
            bizFromVO2.setIsfirst(true);
            bizFromVO2.setProperty("header");
            rootbizFromlist.add(bizFromVO2);
            apiParam.setParamConfig(JSON.toJSONString(rootbizFromlist));
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setHeadList(JSON.toJSONString(headEntityVOS));
            apiParam.setRemarks(fromname+"_查询列表");
            apiParam.setSqlContent(selectListSql.toString());
            tappApiconfigService.saveApiConfigInfo(apiParam);
            if(!"".equals(chartQuerySql.toString())) {
                apiParam.setMethodType(ApiTypeEnum.QUERY);
                apiParam.setMethodName("默认查询统计功能");
                apiParam.setMethodCode("queryChartList");
                apiParam.setPageId(Long.parseLong(r.getData()));
                apiParam.setRemarks(fromname+"_默认查询统计功能");
                apiParam.setSqlContent("select "+chartQuerySql.toString()+",count(*) as "+tableName+"total from "+tableName);
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            for (Map.Entry<String, ExportDetailSqlVO> entry : exportDetailSql.entrySet()) {
                R<String> rDetailPage=  pageService.addPage(entry.getKey(),"");
                entry.getValue().setExportDetailSql( entry.getValue().getExportDetailSql()+whereSqlBuilder.toString()+"</where>");
                apiParam.setMethodType(ApiTypeEnum.LIST);
                apiParam.setMethodName("根据主表查询条件进行导出");
                apiParam.setMethodCode("export");
                apiParam.setPageId(Long.parseLong(rDetailPage.getData()));
                apiParam.setHeadList(JSON.toJSONString(entry.getValue().getHeadEntity()));
                apiParam.setRemarks(fromname+"_根据主表查询条件进行导出");
                apiParam.setSqlContent( entry.getValue().getExportDetailSql());
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            apiParam.setMethodType(ApiTypeEnum.FOROBJECT);
            apiParam.setMethodName("根据Id获取信息");
            apiParam.setMethodCode("getDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_查询列表");
            apiParam.setSqlContent(selectSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
            apiParam.setMethodType(ApiTypeEnum.OPERATE);
            apiParam.setMethodName("根据Id删除");
            apiParam.setMethodCode("deleteInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_根据Id删除");
            apiParam.setSqlContent("delete from "+tableName+" where fid=#{fid}");
            tappApiconfigService.saveApiConfigInfo(apiParam);
            apiParam.setMethodType(ApiTypeEnum.OPERATE);
            apiParam.setMethodName("数据保存接口");
            apiParam.setMethodCode("SaveDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_数据保存接口");
            saveInfoSql.append("<sql param=\"$totalcount\"> select count(*) rowcount from "+tableName+" where fid=#{$tableNewId} </sql>");
            saveInfoSql.append("<sql test=\"$totalcount!=null and $totalcount[0].rowcount>0 \"> "+updatesql+" </sql>");
            saveInfoSql.append("<sql test=\"$totalcount==null or $totalcount[0].rowcount==0 \"> "+insertSql+" </sql>");
            apiParam.setSqlContent(saveInfoSql.toString()+insertChildTableSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
        }
    }

    ///新增修改表结构
    public void AlterTable(List<LayoutModelReq> layoutModelReqList, String tableName) {
        List<ColumnInfoVO>  columnNameList=jdbcTemplate.query("select column_name as columnname ,column_type as columntype  from information_schema.columns where table_schema =(SELECT DATABASE ()) and table_name='"+tableName+"'", new Object[]{}, new BeanPropertyRowMapper<>(ColumnInfoVO.class));
        List<String> newColummNameList = new ArrayList<>();
        newColummNameList.add("fid");
        newColummNameList.add("fcreationdate");
        newColummNameList.add("fcreateby");
        newColummNameList.add("flastupdatedate");
        newColummNameList.add("auditdate");
        newColummNameList.add("flastupdateby");
        newColummNameList.add("auditby");
        newColummNameList.add("fcreatebyname");
        newColummNameList.add("flastupdatebyname");
        newColummNameList.add("auditbyname");
        newColummNameList.add("auditflag");
        newColummNameList.add("openid");
        newColummNameList.add("isimport");
        newColummNameList.add("auditremark");
        StringBuffer selectListSql = new StringBuffer();
        StringBuffer updatesql = new StringBuffer();
        StringBuffer saveInfoSql = new StringBuffer();
//        updatesql.append("update `"+tableName+"` set flastupdateby=#{$user.userId},flastupdatebyname=#{$user.userName},flastupdatedate=now() ");
        updatesql.append("update `"+tableName+"` set auditbyname=#{$user.userName}, auditby=#{$user.userId},auditdate=now(),flastupdatebyname=#{$user.userName}, flastupdateby=#{$user.userId},flastupdatedate=now(), openid=#{openid}, auditflag=#{auditflag}, isimport=#{isimport}, auditremark=#{auditremark} ");
        StringBuffer insertfiled = new StringBuffer();
        StringBuffer insertfiledValue = new StringBuffer();
        StringBuffer insertChildTableSql = new StringBuffer();
        selectListSql.append("select maintable.* from `"+tableName+"` as maintable <where>");
        Map<String,ExportDetailSqlVO> exportDetailSql=new HashMap<>();
        StringBuilder whereSqlBuilder=new StringBuilder();
        ///对表结构进行修改
        List<HeadEntityVO> headEntityVOS=new ArrayList<>();
        StringBuilder chartQuerySql=new StringBuilder();
        List<ParamBizFromVO> paramlist=new ArrayList<>();
        for(LayoutModelReq layoutModelReq:layoutModelReqList) {
            switch (layoutModelReq.getType()) {
                case "gridPanel":
                    for (FormControlReq req:layoutModelReq.getColumns().get(0).getList()) {
                        List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist=getQuestionnaireSqlContent(req,paramlist,headEntityVOS);
                        for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist ) {
                            if(!newColummNameList.contains(questionnaireSqlContentVO.getFiled())) {
                                newColummNameList.add(questionnaireSqlContentVO.getFiled());
                            }
                            StringBuilder alterTableSql=new  StringBuilder();
                            if (questionnaireSqlContentVO != null) {
                                alterTableSql.append("ALTER TABLE " + tableName);
                                Optional<ColumnInfoVO> columnInfo = columnNameList.stream().filter(ee -> ee.getColumnname().equals(questionnaireSqlContentVO.getFiled())).findFirst();
                                if (!columnInfo.equals(Optional.empty())) {
                                    alterTableSql.append("  MODIFY  ");
                                } else {
                                    alterTableSql.append("  ADD  ");
                                }
                                whereSqlBuilder.append(questionnaireSqlContentVO.getSelectListSql());
                                updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                                insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                                if (!req.type.equals("listview")) {
                                    insertfiled.append("," + questionnaireSqlContentVO.getFiled());
                                    alterTableSql.append(questionnaireSqlContentVO.getTableEidtSql());
                                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                        if ("".equals(chartQuerySql.toString())) {
                                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                        } else {
                                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                        }
                                    }
                                    if (columnInfo.equals(Optional.empty()) || (!columnInfo.equals(Optional.empty()) && !columnInfo.get().getColumntype().equals(questionnaireSqlContentVO.getColumnType()))) {
                                        jdbcTemplate.execute(alterTableSql.toString());
                                    }
                                } else {
                                    insertChildTableSql.append(questionnaireSqlContentVO.getInsertChildTableSql());
                                    saveInfoSql.append(questionnaireSqlContentVO.getSaveInfoSql());
                                    exportDetailSql.put(req.options.controlId, new ExportDetailSqlVO());
                                    exportDetailSql.get(req.options.controlId).setExportDetailSql("select detailtable.* from " + req.options.controlId + " as detailtable inner join `" + tableName + "` as maintable on detailtable.fparentid=maintable.fid  <where> <if test=\"fcreateby!=null and fcreateby!=''\">and maintable.fcreateby=#{fcreateby} </if>");
                                    exportDetailSql.get(req.options.controlId).setHeadEntity(questionnaireSqlContentVO.getHeadEntity());
                                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                        if ("".equals(chartQuerySql.toString())) {
                                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                        } else {
                                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case "collapse":
                case "tabs":
                    for (LayoutModelPanelistReq layoutModelPanelistReq:layoutModelReq.getOptions().getPanelist()){
                        for (FormControlReq req:layoutModelPanelistReq.getColumns().get(0).getList()) {
                            List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist1=getQuestionnaireSqlContent(req,paramlist,headEntityVOS);
                            for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist1 ) {
                                if(!newColummNameList.contains(questionnaireSqlContentVO.getFiled())) {
                                    newColummNameList.add(questionnaireSqlContentVO.getFiled());
                                }
                                StringBuilder alterTableSql=new  StringBuilder();
                                if (questionnaireSqlContentVO != null) {
                                    alterTableSql.append("ALTER TABLE " + tableName);
                                    Optional<ColumnInfoVO> columnInfo = columnNameList.stream().filter(ee -> ee.getColumnname().equals(questionnaireSqlContentVO.getFiled())).findFirst();
                                    if (!columnInfo.equals(Optional.empty())) {
                                        alterTableSql.append("  MODIFY  ");
                                    } else {
                                        alterTableSql.append("  ADD  ");
                                    }
                                    whereSqlBuilder.append(questionnaireSqlContentVO.getSelectListSql());
                                    updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                                    insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                                    if (!req.type.equals("listview")) {
                                        insertfiled.append("," + questionnaireSqlContentVO.getFiled());
                                        alterTableSql.append(questionnaireSqlContentVO.getTableEidtSql());
                                        if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                            if ("".equals(chartQuerySql.toString())) {
                                                chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                            } else {
                                                chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                            }
                                        }
                                        if (columnInfo.equals(Optional.empty()) || (!columnInfo.equals(Optional.empty()) && !columnInfo.get().getColumntype().equals(questionnaireSqlContentVO.getColumnType()))) {
                                            jdbcTemplate.execute(alterTableSql.toString());
                                        }
                                    } else {
                                        insertChildTableSql.append(questionnaireSqlContentVO.getInsertChildTableSql());
                                        saveInfoSql.append(questionnaireSqlContentVO.getSaveInfoSql());
                                        exportDetailSql.put(req.options.controlId, new ExportDetailSqlVO());
                                        exportDetailSql.get(req.options.controlId).setExportDetailSql("select detailtable.* from " + req.options.controlId + " as detailtable inner join `" + tableName + "` as maintable on detailtable.fparentid=maintable.fid  <where> <if test=\"fcreateby!=null and fcreateby!=''\">and maintable.fcreateby=#{fcreateby} </if>");
                                        exportDetailSql.get(req.options.controlId).setHeadEntity(questionnaireSqlContentVO.getHeadEntity());
                                        if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                                            if ("".equals(chartQuerySql.toString())) {
                                                chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                                            } else {
                                                chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                    break;
                default:break;
            }
        }
        ///获取删除表列字段集合
        List<ColumnInfoVO> delColumnList = columnNameList.stream().filter(ee -> !newColummNameList.contains(ee.getColumnname())).collect(Collectors.toList());
        delColumnList.forEach(ee -> {
            jdbcTemplate.execute("ALTER TABLE " + tableName + " DROP COLUMN " + ee.getColumnname() + "; ");
        });
        jdbcTemplate.execute("ALTER TABLE " + tableName + " comment '" + fromname + "'; ");
        whereSqlBuilder.append("<if test=\"fcreationdate!=null and fcreationdate.size()>0\">and maintable.fcreationdate between #{fcreationdate[0]} and #{fcreationdate[1]}  </if>");
        whereSqlBuilder.append("<if test=\"fcreatebyname!=null and fcreatebyname!=''\">and maintable.fcreatebyname  like CONCAT('%',#{fcreatebyname},'%')  </if>");
        whereSqlBuilder.append("<if test=\"auditbyname!=null and auditbyname!=''\">and maintable.auditbyname  like CONCAT('%',#{auditbyname},'%')  </if>");
        whereSqlBuilder.append("<if test=\"auditflag!=null and auditflag!=''\">and maintable.auditflag =#{auditflag}  </if>");
        whereSqlBuilder.append("<if test=\"openid!=null and openid!=''\">and maintable.openid  = #{openid}  </if>");
        whereSqlBuilder.append("<if test=\"isimport!=null and isimport!=''\">and maintable.isimport  = #{isimport}  </if>");
        whereSqlBuilder.append("<if test=\"auditremark!=null and auditremark!=''\">and maintable.auditremark  = #{auditremark}  </if>");
        selectListSql.append(whereSqlBuilder.toString());
        GetDefaultHeadEntity(headEntityVOS,paramlist);
        selectListSql.append("</where>");
        updatesql.append(" where fid=#{$tableNewId}");
        String selectSql="select *from "+tableName+" where fid=#{fid}";
        String insertSql="insert into "+tableName+"(fid,fcreationdate,fcreateby,fcreatebyname,flastupdatedate,flastupdateby,flastupdatebyname,auditflag,openid,isimport,auditremark "+insertfiled.toString()+") values(#{$tableNewId},now(),#{$user.userId},#{$user.userName},now(),#{$user.userId},#{$user.userName} ,#{auditflag} ,#{openid} ,#{isimport} ,#{auditremark}"+insertfiledValue.toString()+")";
        R<String> r= pageService.addPage(tableName,fromname);
        SaveApiConfigReq apiParam=new SaveApiConfigReq();
        if(r.getCode()==0) {
            apiParam.setMethodType(ApiTypeEnum.LIST);
            apiParam.setMethodName("查询列表");
            apiParam.setMethodCode("querylist");
            apiParam.setPageId(Long.parseLong( r.getData()));
            apiParam.setHeadList(JSON.toJSONString(headEntityVOS));
            apiParam.setRemarks(fromname+"_查询列表");
            List<ParamBizFromVO> rootbizFromlist=new ArrayList<>();
            ParamBizFromVO bizFromVO=new ParamBizFromVO();
            bizFromVO.setParamname("Post参数");
            bizFromVO.setVariabletype("Object");
            bizFromVO.setIsfirst(true);
            bizFromVO.setProperty("body");
            bizFromVO.setChildren(paramlist);
            rootbizFromlist.add(bizFromVO);
            ParamBizFromVO bizFromVO1=new ParamBizFromVO();
            bizFromVO1.setParamname("URL参数");
            bizFromVO1.setVariabletype("Object");
            bizFromVO1.setIsfirst(true);
            bizFromVO1.setProperty("$query");
            rootbizFromlist.add(bizFromVO1);
            ParamBizFromVO bizFromVO2=new ParamBizFromVO();
            bizFromVO2.setParamname("header头部参数");
            bizFromVO2.setVariabletype("Object");
            bizFromVO2.setIsfirst(true);
            bizFromVO2.setProperty("header");
            rootbizFromlist.add(bizFromVO2);
            apiParam.setParamConfig(JSON.toJSONString(rootbizFromlist));
            apiParam.setSqlContent(selectListSql.toString());
            tappApiconfigService.saveApiConfigInfo(apiParam);
            if(!"".equals(chartQuerySql.toString())) {
                apiParam.setMethodType(ApiTypeEnum.QUERY);
                apiParam.setMethodName("默认查询统计功能");
                apiParam.setMethodCode("queryChartList");
                apiParam.setPageId(Long.parseLong(r.getData()));
                apiParam.setRemarks(fromname+"_默认查询统计功能");
                apiParam.setSqlContent("select "+chartQuerySql.toString()+",count(*) as "+tableName+"total from "+tableName);
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            for (Map.Entry<String, ExportDetailSqlVO> entry : exportDetailSql.entrySet()) {
                R<String> rDetailPage=  pageService.addPage(entry.getKey(),"");
                entry.getValue().setExportDetailSql( entry.getValue().getExportDetailSql()+whereSqlBuilder.toString()+"</where>");
                apiParam.setMethodType(ApiTypeEnum.LIST);
                apiParam.setMethodName("根据主表查询条件进行导出");
                apiParam.setMethodCode("export");
                apiParam.setPageId(Long.parseLong(rDetailPage.getData()));
                apiParam.setHeadList(JSON.toJSONString(entry.getValue().getHeadEntity()));
                apiParam.setRemarks(fromname+"_根据主表查询条件进行导出");
                apiParam.setSqlContent( entry.getValue().getExportDetailSql());
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            apiParam.setMethodType(ApiTypeEnum.FOROBJECT);
            apiParam.setMethodName("根据Id获取信息");
            apiParam.setMethodCode("getDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_根据Id获取信息");
            apiParam.setSqlContent(selectSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
            apiParam.setMethodType(ApiTypeEnum.OPERATE);
            apiParam.setMethodName("根据Id删除");
            apiParam.setMethodCode("deleteInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_根据Id删除");
            apiParam.setSqlContent("delete from "+tableName+" where fid=#{fid}");
            tappApiconfigService.saveApiConfigInfo(apiParam);
            apiParam.setMethodType(ApiTypeEnum.OPERATE);
            apiParam.setMethodName("数据保存接口");
            apiParam.setMethodCode("SaveDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"_数据保存接口");
            saveInfoSql.append("<sql param=\"$totalcount\"> select count(*) rowcount from "+tableName+" where fid=#{$tableNewId} </sql>");
            saveInfoSql.append("<sql test=\"$totalcount!=null and $totalcount[0].rowcount>0 \"> "+updatesql+" </sql>");
            saveInfoSql.append("<sql test=\"$totalcount==null or $totalcount[0].rowcount==0 \"> "+insertSql+" </sql>");
            apiParam.setSqlContent(saveInfoSql.toString()+insertChildTableSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
        }
    }

    void GetDefaultHeadEntity(List<HeadEntityVO> headEntityVOS,List<ParamBizFromVO> paramlist) {
        HeadEntityVO headEntityVO2 = new HeadEntityVO();
        headEntityVO2.setBindname("fcreationdate");
        headEntityVO2.setHeadname("创建时间");
        headEntityVOS.add(headEntityVO2);
        HeadEntityVO headEntityVO3 = new HeadEntityVO();
        headEntityVO3.setBindname("fcreatebyname");
        headEntityVO3.setHeadname("创建人");
        headEntityVOS.add(headEntityVO3);
        HeadEntityVO headEntityVO4 = new HeadEntityVO();
        headEntityVO4.setBindname("auditflag");
        headEntityVO4.setHeadname("审核状态");
        headEntityVOS.add(headEntityVO4);
        HeadEntityVO headEntityVO5 = new HeadEntityVO();
        headEntityVO5.setBindname("openid");
        headEntityVO5.setHeadname("微信标识");
        headEntityVOS.add(headEntityVO5);
        HeadEntityVO headEntityVO6 = new HeadEntityVO();
        headEntityVO6.setBindname("isimport");
        headEntityVO6.setHeadname("导入状态");
        headEntityVOS.add(headEntityVO6);
        HeadEntityVO headEntityVO7 = new HeadEntityVO();
        headEntityVO7.setBindname("auditremark");
        headEntityVO7.setHeadname("审核备注");
        headEntityVOS.add(headEntityVO7);
        HeadEntityVO headEntityVO8 = new HeadEntityVO();
        headEntityVO8.setBindname("auditbyname");
        headEntityVO8.setHeadname("审核人");
        headEntityVOS.add(headEntityVO8);
        ParamBizFromVO item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("fcreatebyname");
        item.setParamname("创建人");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("fcreationdate");
        item.setParamname("创建时间");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("auditflag");
        item.setParamname("审核状态");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("isimport");
        item.setParamname("导入标志");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("openid");
        item.setParamname("微信标识");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("auditremark");
        item.setParamname("审核备注");
        paramlist.add(item);

        item=new ParamBizFromVO();
        item.setRequired(false);
        item.setVariabletype("常量");
        item.setProperty("auditbyname");
        item.setParamname("审核人");
        paramlist.add(item);
    }

    List<QuestionnaireSqlContentVO>  getQuestionnaireSqlContent(FormControlReq req,  List<ParamBizFromVO> paramlist,List<HeadEntityVO> headEntityVOList) {
        List<QuestionnaireSqlContentVO> returnQuestionnairelist=new ArrayList<>();
        QuestionnaireSqlContentVO questionnaireSqlContentVO =getNewQuestionnaireContent();
        questionnaireSqlContentVO.setFiled(req.options.filed);
        HeadEntityVO headEntityVO=new HeadEntityVO();
        headEntityVO.setBindname(req.options.filed);
        headEntityVO.setHeadname(req.title);
        if (!req.type.equals("listview")) {
            headEntityVOList.add(headEntityVO);
        }
        ParamBizFromVO item=new ParamBizFromVO();
        switch (req.type) {
            case "input":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " like CONCAT('%',#{" + req.options.filed + "},'%') </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setRequired(false);
                item.setVariabletype("常量");
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "textarea":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` text   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " like CONCAT('%',#{" + req.options.filed + "},'%') </if>");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "select":
                if(req.options.getIsMulty()!=null&&req.options.getIsMulty().equals(true)) {
                    questionnaireSqlContentVO.getTableEidtSql().append("`"+req.options.filed+"` varchar(500)   DEFAULT NULL ");
                    questionnaireSqlContentVO.setColumnType("varchar(500)");
                    questionnaireSqlContentVO.getSelectListSql().append("<if test=\""+req.options.filed+"!=null and "+req.options.filed+"!=''\">and maintable."+req.options.filed+" like CONCAT('%', #{"+req.options.filed+"},'%') </if>");
                }else{
                    questionnaireSqlContentVO.getTableEidtSql().append("`"+req.options.filed+"` varchar(50)   DEFAULT NULL ");
                    questionnaireSqlContentVO.setColumnType("varchar(50)");
                    questionnaireSqlContentVO.getSelectListSql().append("<if test=\""+req.options.filed+"!=null and "+req.options.filed+"!=''\">and maintable."+req.options.filed+" =  #{"+req.options.filed+"} </if>");
                }
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(req.options.getIsMulty(),req.options.filed,req.options.getOptions()));
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "date":
                switch (req.options.modeltype) {
                    case "date":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + ".size()>0\">and " + req.options.filed + " between #{" + req.options.filed + "[0]} and #{" + req.options.filed + "[1]}  </if>");
                        break;
                    case "year":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and date_format(" + req.options.filed + ",'%Y') = date_format(#{" + req.options.filed + "},'%Y') </if>");
                        break;
                    case "month":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and date_format(" + req.options.filed + ",'%Y%m') = date_format(#{" + req.options.filed + "},'%Y%m') </if>");
                        break;
                    case "week":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "`  varchar(50)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + ".size()>0\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                        break;
                    case "range":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "`  varchar(50)   DEFAULT NULL ");
                        // selectListSql.append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and date_format(" + req.options.filed + ",'%Y') = date_format(#{" + req.options.filed + "},'%Y') </if>");
                        break;
                    default:
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                        break;

                }
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                break;
            case "radio":
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(false,req.options.filed,req.options.getOptions()));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "checkbox":
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(true,req.options.filed,req.options.getOptions()));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " like CONCAT('%', #{" + req.options.filed + "},'%') </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "switch":
                questionnaireSqlContentVO.getChartQuerySql().append(chartswitchFiledSql(req.options.filed));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` tinyint(1)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!='' and " + req.options.filed + "!=''\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "==true?1:0}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "==true?1:0}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "slider":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.options.filed));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + ".size()>0\">and " + req.options.filed + " between  #{" + req.options.filed + "[0]} and #{" + req.options.filed + "[1]} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "colorpicker":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "editor":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` text   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " like CONCAT('%', #{" + req.options.filed + "},'%') </if>");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "rate":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.options.filed));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and {0} = #{" + req.options.filed + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "inputnumber":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.options.filed));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and " + req.options.filed + " = #{" + req.options.filed + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "timepicker":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + ".size()>0\">and " + req.options.filed + " between #{" + req.options.filed + "[0]}  and #{" + req.options.filed + "[1]} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "upFilesComp":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                break;
            case"user":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!='' \">and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"organ":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"department":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"role":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!='' \" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"imgage":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                break;
            case"cityselector":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!='' \" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(150)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"datarelevance":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                item.setFormId(querybyTableName(req.options.sourceApi));
                paramlist.add(item);
                returnQuestionnairelist.add(getNewQuestionnaireContent());
                returnQuestionnairelist.get(0).getTableEidtSql().append("`" +req.options.filed+ "_text` varchar(250)   DEFAULT NULL ");
                returnQuestionnairelist.get(0).getInsertFiledSql().append(",#{" + req.options.filed + "_text}");
                returnQuestionnairelist.get(0).getUpdateFiledSql().append(" ," +req.options.filed + "_text=#{"  + req.options.filed + "_text}");
                returnQuestionnairelist.get(0).setFiled(req.options.filed+ "_text");
                headEntityVO.setBindname(req.options.filed+ "_text");
                headEntityVO.setHeadname(req.title);
                headEntityVO.setFromId(item.getFormId());
                break;
            case"fromlable":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(2000)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                break;
            case"autonumber":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\" >and " + req.options.filed + " like CONCAT('%', #{" + req.options.filed + "},'%')  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(100)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"signname":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(200)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                break;
            case"locationmap":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\" >and " + req.options.filed + " like CONCAT('%', #{" + req.options.filed + "},'%')  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.options.filed + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + req.options.filed + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.options.filed + "=#{" + req.options.filed + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.options.filed);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "listview":
                TableChildInfoVO tableChildInfoVO= TableChildEidt(req.getOptions().getColumns(),req.options.controlId,req.title);
                questionnaireSqlContentVO.setHeadEntity(tableChildInfoVO.getHeadEntity());
                questionnaireSqlContentVO.getInsertChildTableSql().append("<for  forname=\""+req.options.controlId+"\">"+tableChildInfoVO.getSaveInfoSql().toString()+"</for>");
                questionnaireSqlContentVO.getSaveInfoSql().append("<sql test=\"del"+req.options.controlId+"!=null and  del"+req.options.controlId+".size()>0 \"> delete from  "+req.options.controlId+" where #{del"+req.options.controlId+"} like CONCAT('%',fid,'%')</sql>");
                break;
            default:
                questionnaireSqlContentVO = null;
                break;
        }
        if (!req.type.equals("listview")&&questionnaireSqlContentVO!=null) {
            questionnaireSqlContentVO.getTableEidtSql().append("COMMENT '" + req.title.replace("'", "''") + "' ");

        }
        returnQuestionnairelist.add(questionnaireSqlContentVO);
        return returnQuestionnairelist;
    }
    QuestionnaireSqlContentVO getNewQuestionnaireContent(){
        QuestionnaireSqlContentVO questionnaireSqlContentVO = new QuestionnaireSqlContentVO();
        questionnaireSqlContentVO.setInsertChildTableSql(new StringBuilder());
        questionnaireSqlContentVO.setInsertFiledSql(new StringBuilder());
        questionnaireSqlContentVO.setSaveInfoSql(new StringBuilder());
        questionnaireSqlContentVO.setSelectListSql(new StringBuilder());
        questionnaireSqlContentVO.setTableEidtSql(new StringBuilder());
        questionnaireSqlContentVO.setUpdateFiledSql(new StringBuilder());
        questionnaireSqlContentVO.setChartQuerySql(new StringBuilder());
        return questionnaireSqlContentVO;
    }
    String chartFiledSql(boolean IsMulty,String filed,List<String> optionItemReqList){
        StringBuilder sqlBuilder=new StringBuilder();
        if(optionItemReqList!=null){
            if(IsMulty){
                for (String req:optionItemReqList) {
                    sqlBuilder.append(" sum(case when "+filed+" like '%"+req+"%' then 1 else 0 end) as '"+filed+req+"',");
                }
            }else{
                for (String req:optionItemReqList) {
                    sqlBuilder.append(" sum(case when "+filed+"='"+req+"'then 1 else 0 end) as '"+filed+req+"',");
                }
            }
        }
        if(sqlBuilder.length()>0){
            return sqlBuilder.toString().substring(0, sqlBuilder.toString().lastIndexOf(","));
        }
        return  sqlBuilder.toString();
    }
    String chartswitchFiledSql(String filed){
        StringBuilder sqlBuilder=new StringBuilder();
        sqlBuilder.append(" sum(case when "+filed+" = 1 then 1 else 0 end) as '"+filed+"yes',");
        sqlBuilder.append(" sum(case when "+filed+" != 1 then 1 else 0 end) as '"+filed+"no'");
        return  sqlBuilder.toString();
    }
    String chartNumberFiledSql(String filed){
        StringBuilder sqlBuilder=new StringBuilder();
        sqlBuilder.append(" sum("+filed+") as '"+filed+"sum',");
        sqlBuilder.append(" min("+filed+") as '"+filed+"min',");
        sqlBuilder.append(" max("+filed+") as '"+filed+"max',");
        sqlBuilder.append(" avg("+filed+") as '"+filed+"svg'");
        return  sqlBuilder.toString();
    }
    /**
     * 子表控件数据保存
     * @param formControlReqList 明细控件对象
     * @param parenttableName 父级表名
     */
    public TableChildInfoVO TableChildEidt(List<FormListViewControlReq> formControlReqList, String  parenttableName, String listtitle){
        final int rowCount = jdbcTemplate.queryForObject("SELECT count(*) totalcount FROM information_schema.TABLES WHERE table_schema =(SELECT DATABASE ()) and table_name='"+parenttableName+"'", Integer.class);
        if(rowCount==0){
            return   CreateChildTable(formControlReqList,parenttableName, listtitle);
        }else{
            return   AlterChildTable(formControlReqList,parenttableName, listtitle);
        }

    }
    ///新增创建子表
    public TableChildInfoVO CreateChildTable(List<FormListViewControlReq> formControlReqList, String tableName,String listtitle) {
        String createTableSqlstr="CREATE TABLE `"+tableName+"`  (\n" +
                "  `fid` varchar(20) NOT NULL ,\n" +
                "  `fcreationdate` datetime(0) DEFAULT NULL,\n" +
                "  `fcreatebyname` varchar(100)   DEFAULT NULL,\n" +
                "  `fcreateby` varchar(20)   DEFAULT NULL,\n" +
                "  `flastupdatedate` datetime(0) DEFAULT NULL,\n" +
                "  `fparentid` varchar(20) NOT NULL,\n" +
                "  `flastupdateby` varchar(20)   DEFAULT NULL,\n"+
                "  `flastupdatebyname` varchar(100)   DEFAULT NULL,\n";
        StringBuffer createTableSql= new StringBuffer();
        StringBuffer selectListSql = new StringBuffer();
        StringBuffer updatesql = new StringBuffer();
        updatesql.append("update `"+tableName+"` set flastupdatebyname=#{$user.userName} ,flastupdateby=#{$user.userId},flastupdatedate=now() ");
        StringBuffer insertfiled = new StringBuffer();
        StringBuffer insertfiledValue = new StringBuffer();
        selectListSql.append("select *from `"+tableName+"` <where> <if test=\"fcreateby!=null and fcreateby!=''\">and fcreateby=#{fcreateby} </if><if test=\"$tableNewId!=null and $tableNewId!=''\">and fparentid=#{$tableNewId} </if>");
        List<HeadEntityVO> headEntityVOS=new ArrayList<>();
        StringBuilder chartQuerySql=new StringBuilder();
        int idex=0;
        List<ParamBizFromVO> paramlist=new ArrayList<>();
        for (FormListViewControlReq req:formControlReqList) {
            List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist=getChildQuestionnaireSqlContent(req,tableName,paramlist,headEntityVOS);
            for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist ) {
                if (questionnaireSqlContentVO != null) {
                    insertfiled.append("," +questionnaireSqlContentVO.getFiled());
                    createTableSql.append(questionnaireSqlContentVO.getTableEidtSql() + ",");
                    selectListSql.append(questionnaireSqlContentVO.getSelectListSql());
                    updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                    insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                        if ("".equals(chartQuerySql.toString())) {
                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                        } else {
                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                        }
                    }
                }
            }
            idex++;
        }
        GetDefaultHeadEntity(headEntityVOS,paramlist);
        selectListSql.append("</where>");
        createTableSqlstr=createTableSqlstr+  createTableSql.toString()+"   PRIMARY KEY (`fid`)) comment='"+fromname+"-"+listtitle+"';";
        String selectSql="select *from "+tableName+" where fid=#{fid}";
        updatesql.append(" where fid=#{"+tableName+"[$index0].fid}");
        String insertSql="insert into "+tableName+"(fid,fcreationdate,fcreateby,fcreatebyname,flastupdatedate,flastupdateby,flastupdatebyname,fparentid "+insertfiled.toString()+") values(#{$newid},now(),#{$user.userId},#{$user.userName},now(),#{$user.userId},#{$user.userName},#{$tableNewId} "+insertfiledValue.toString()+")";
        jdbcTemplate.execute(createTableSqlstr);
        R<String> r= pageService.addPage(tableName,fromname+"-"+listtitle);
        SaveApiConfigReq apiParam=new SaveApiConfigReq();
        if(r.getCode()==0) {
            apiParam.setMethodType(ApiTypeEnum.LIST);
            apiParam.setMethodName(fromname+"-"+listtitle+"列表");
            apiParam.setMethodCode("querylist");
            apiParam.setPageId(Long.parseLong( r.getData()));
            apiParam.setHeadList(JSON.toJSONString(headEntityVOS));
            apiParam.setRemarks(fromname+"-"+listtitle+"列表");
            List<ParamBizFromVO> rootbizFromlist=new ArrayList<>();
            ParamBizFromVO bizFromVO=new ParamBizFromVO();
            bizFromVO.setParamname("Post参数");
            bizFromVO.setVariabletype("Object");
            bizFromVO.setIsfirst(true);
            bizFromVO.setProperty("body");
            bizFromVO.setChildren(paramlist);
            rootbizFromlist.add(bizFromVO);
            ParamBizFromVO bizFromVO1=new ParamBizFromVO();
            bizFromVO1.setParamname("URL参数");
            bizFromVO1.setVariabletype("Object");
            bizFromVO1.setIsfirst(true);
            bizFromVO1.setProperty("query");
            rootbizFromlist.add(bizFromVO1);
            ParamBizFromVO bizFromVO2=new ParamBizFromVO();
            bizFromVO2.setParamname("header头部参数");
            bizFromVO2.setVariabletype("Object");
            bizFromVO2.setIsfirst(true);
            bizFromVO2.setProperty("header");
            rootbizFromlist.add(bizFromVO2);
            apiParam.setParamConfig(JSON.toJSONString(rootbizFromlist));
            apiParam.setSqlContent(selectListSql.toString());
            tappApiconfigService.saveApiConfigInfo(apiParam);
            if(!"".equals(chartQuerySql.toString())) {
                apiParam.setMethodType(ApiTypeEnum.QUERY);
                apiParam.setMethodName("统计图查询sql");
                apiParam.setMethodCode("queryChartList");
                apiParam.setPageId(Long.parseLong(r.getData()));
                apiParam.setRemarks(fromname+"-"+listtitle+"统计图查询sql");
                apiParam.setSqlContent("select "+chartQuerySql.toString()+",count(*) as "+tableName+"total from "+tableName);
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            apiParam.setMethodType(ApiTypeEnum.QUERY);
            apiParam.setMethodName("根据Id删除");
            apiParam.setMethodCode("deleteInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"-"+listtitle+"根据Id删除");
            apiParam.setSqlContent("delete from "+tableName+" where fid=#{fid}");
            tappApiconfigService.saveApiConfigInfo(apiParam);
            apiParam.setMethodType(ApiTypeEnum.FOROBJECT);
            apiParam.setMethodName("根据Id获取信息");
            apiParam.setMethodCode("getDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"-"+listtitle+"根据Id获取信息");
            apiParam.setSqlContent(selectSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
//            apiParam.setMethodType(ApiTypeEnum.OPERATE);
//            apiParam.setMethodName("数据保存接口");
//            apiParam.setMethodCode("SaveDataInfo");
//            apiParam.setPageId(Long.parseLong(r.getData()));
//            apiParam.setRemarks("数据保存接口");
//            apiParam.setSqlContent(insertSql);
//            tappApiconfigService.saveApiConfigInfo(apiParam);
        }
        StringBuffer saveInfoSql = new StringBuffer();
        saveInfoSql.append("<sql param=\""+tableName+"[$index0].$totalcount\"> select count(*) rowcount from "+tableName+" where fid=#{"+tableName+"[$index0].fid} </sql>");
        saveInfoSql.append("<sql test=\""+tableName+"[$index0].$totalcount!=null and "+tableName+"[$index0].$totalcount[0].rowcount>0 \"> "+updatesql+" </sql>");
        saveInfoSql.append("<sql test=\""+tableName+"[$index0].$totalcount==null or "+tableName+"[$index0].$totalcount[0].rowcount==0 \"> "+insertSql+" </sql>");
        TableChildInfoVO tableChildInfoVO=new TableChildInfoVO();
        tableChildInfoVO.setHeadEntity(headEntityVOS);
        tableChildInfoVO.setSaveInfoSql(saveInfoSql);
        return tableChildInfoVO;
    }

    ///新增修改子表结构
    public TableChildInfoVO AlterChildTable(List<FormListViewControlReq> formControlReqList, String tableName,String listtitle) {
        List<ColumnInfoVO>  columnNameList=jdbcTemplate.query("select column_name as columnname ,column_type as columntype  from information_schema.columns where table_schema =(SELECT DATABASE ()) and table_name='"+tableName+"'", new Object[]{}, new BeanPropertyRowMapper<>(ColumnInfoVO.class));
        List<String> newColummNameList= new ArrayList<>();
        newColummNameList.add("fid");
        newColummNameList.add("fcreationdate");
        newColummNameList.add("fcreateby");
        newColummNameList.add("fcreatebyname");
        newColummNameList.add("flastupdatedate");
        newColummNameList.add("flastupdateby");
        newColummNameList.add("flastupdatebyname");
        newColummNameList.add("fparentid");
        ///获取删除表列字段集合
        jdbcTemplate.execute("ALTER TABLE " + tableName + " comment '" + fromname+"-"+listtitle + "'; ");
        StringBuffer selectListSql = new StringBuffer();
        StringBuffer updatesql = new StringBuffer();
        updatesql.append("update `"+tableName+"` set flastupdatebyname=#{$user.userName} ,flastupdateby=#{$user.userId},flastupdatedate=now() ");
        StringBuffer insertfiled = new StringBuffer();
        StringBuffer insertfiledValue = new StringBuffer();
        selectListSql.append("select *from `"+tableName+"` <where> <if test=\"fcreateby!=null and fcreateby!=''\">and fcreateby=#{fcreateby} </if><if test=\"$tableNewId!=null and $tableNewId!=''\">and fparentid=#{$tableNewId} </if>");
        ///对表结构进行修改
        List<HeadEntityVO> headEntityVOS=new ArrayList<>();
        StringBuilder chartQuerySql=new StringBuilder();
        List<ParamBizFromVO> paramlist=new ArrayList<>();
        int idex=0;
        for (FormListViewControlReq req:formControlReqList) {
            List<QuestionnaireSqlContentVO>   questionnaireSqlContentVOlist=getChildQuestionnaireSqlContent(req,tableName,paramlist,headEntityVOS);
            for(QuestionnaireSqlContentVO questionnaireSqlContentVO:questionnaireSqlContentVOlist ) {
                if(!newColummNameList.contains(questionnaireSqlContentVO.getFiled())) {
                    newColummNameList.add(questionnaireSqlContentVO.getFiled());
                }
                if (questionnaireSqlContentVO != null) {
                    insertfiled.append("," + questionnaireSqlContentVO.getFiled());
                    StringBuilder alterTableSql = new StringBuilder();
                    alterTableSql.append("ALTER TABLE " + tableName);
                    Optional<ColumnInfoVO> columnInfo = columnNameList.stream().filter(ee -> ee.getColumnname().equals(questionnaireSqlContentVO.getFiled())).findFirst();
                    if (!columnInfo.equals(Optional.empty())) {
                        alterTableSql.append("  MODIFY  ");
                    } else {
                        alterTableSql.append("  ADD  ");
                    }
                    alterTableSql.append(questionnaireSqlContentVO.getTableEidtSql());
                    if (columnInfo.equals(Optional.empty()) || (!columnInfo.equals(Optional.empty()) && !columnInfo.get().getColumntype().equals(questionnaireSqlContentVO.getColumnType()))) {
                        jdbcTemplate.execute(alterTableSql.toString());
                    }
                    selectListSql.append(questionnaireSqlContentVO.getSelectListSql());
                    updatesql.append(questionnaireSqlContentVO.getUpdateFiledSql());
                    insertfiledValue.append(questionnaireSqlContentVO.getInsertFiledSql());
                    if (!"".equals(questionnaireSqlContentVO.getChartQuerySql().toString())) {
                        if ("".equals(chartQuerySql.toString())) {
                            chartQuerySql.append(questionnaireSqlContentVO.getChartQuerySql().toString());
                        } else {
                            chartQuerySql.append("," + questionnaireSqlContentVO.getChartQuerySql().toString());
                        }
                    }
                }
            }
            idex++;

        }
        List<ColumnInfoVO>  delColumnList= columnNameList.stream().filter(ee->!newColummNameList.contains(ee.getColumnname())).collect(Collectors.toList());
        delColumnList.forEach(ee->{
            jdbcTemplate.execute("ALTER TABLE "+tableName+" DROP COLUMN "+ee.getColumnname()+"; ");
        });
        GetDefaultHeadEntity(headEntityVOS,paramlist);
        selectListSql.append("</where>");
        updatesql.append(" where fid=#{"+tableName+"[$index0].fid}");
        String selectSql="select *from "+tableName+" where fid=#{fid}";
        String insertSql="insert into "+tableName+"(fid,fcreationdate,fcreateby,fcreatebyname,flastupdatedate,flastupdateby,flastupdatebyname,fparentid "+insertfiled.toString()+") values(#{$newid},now(),#{$user.userId},#{$user.userName},now(),#{$user.userId},#{$user.userName},#{$tableNewId} "+insertfiledValue.toString()+")";
        R<String> r= pageService.addPage(tableName,fromname+"-"+listtitle);
        SaveApiConfigReq apiParam=new SaveApiConfigReq();
        if(r.getCode()==0) {
            apiParam.setMethodType(ApiTypeEnum.LIST);
            apiParam.setMethodName("查询列表");
            apiParam.setMethodCode("querylist");
            apiParam.setPageId(Long.parseLong(r.getData()));
//            apiParam.setParamConfig(JSON.toJSONString(paramlist) );
            apiParam.setHeadList(JSON.toJSONString(headEntityVOS));
            apiParam.setRemarks(fromname+"-"+listtitle+"列表");
            List<ParamBizFromVO> rootbizFromlist=new ArrayList<>();
            ParamBizFromVO bizFromVO=new ParamBizFromVO();
            bizFromVO.setParamname("Post参数");
            bizFromVO.setVariabletype("Object");
            bizFromVO.setIsfirst(true);
            bizFromVO.setProperty("body");
            bizFromVO.setChildren(paramlist);
            rootbizFromlist.add(bizFromVO);
            ParamBizFromVO bizFromVO1=new ParamBizFromVO();
            bizFromVO1.setParamname("URL参数");
            bizFromVO1.setVariabletype("Object");
            bizFromVO1.setIsfirst(true);
            bizFromVO1.setProperty("query");
            rootbizFromlist.add(bizFromVO1);
            ParamBizFromVO bizFromVO2=new ParamBizFromVO();
            bizFromVO2.setParamname("header头部参数");
            bizFromVO2.setVariabletype("Object");
            bizFromVO2.setIsfirst(true);
            bizFromVO2.setProperty("header");
            rootbizFromlist.add(bizFromVO2);
            apiParam.setParamConfig(JSON.toJSONString(rootbizFromlist));
            apiParam.setSqlContent(selectListSql.toString());
            tappApiconfigService.saveApiConfigInfo(apiParam);
            if(!"".equals(chartQuerySql.toString())) {
                apiParam.setMethodType(ApiTypeEnum.QUERY);
                apiParam.setMethodName("统计图查询sql");
                apiParam.setMethodCode("queryChartList");
                apiParam.setPageId(Long.parseLong(r.getData()));
                apiParam.setRemarks(fromname+"-"+listtitle+"统计图查询sql");
                apiParam.setSqlContent("select "+chartQuerySql.toString()+",count(*) as "+tableName+"total from "+tableName);
                tappApiconfigService.saveApiConfigInfo(apiParam);
            }
            apiParam.setMethodType(ApiTypeEnum.FOROBJECT );
            apiParam.setMethodName("根据Id获取信息");
            apiParam.setMethodCode("getDataInfo");
            apiParam.setPageId(Long.parseLong(r.getData()));
            apiParam.setRemarks(fromname+"-"+listtitle+"根据Id获取信息");
            apiParam.setSqlContent(selectSql);
            tappApiconfigService.saveApiConfigInfo(apiParam);
        }
        StringBuffer saveInfoSql = new StringBuffer();
        saveInfoSql.append("<sql param=\""+tableName+"[$index0].$totalcount\"> select count(*) rowcount from "+tableName+" where fid=#{"+tableName+"[$index0].fid} </sql>");
        saveInfoSql.append("<sql test=\""+tableName+"[$index0].$totalcount!=null and "+tableName+"[$index0].$totalcount[0].rowcount>0 \"> "+updatesql+" </sql>");
        saveInfoSql.append("<sql test=\""+tableName+"[$index0].$totalcount==null or "+tableName+"[$index0].$totalcount[0].rowcount==0 \"> "+insertSql+" </sql>");
        TableChildInfoVO tableChildInfoVO=new TableChildInfoVO();
        tableChildInfoVO.setHeadEntity(headEntityVOS);
        tableChildInfoVO.setSaveInfoSql(saveInfoSql);
        return tableChildInfoVO;
    }

    List<QuestionnaireSqlContentVO>  getChildQuestionnaireSqlContent(FormListViewControlReq req, String tableName,List<ParamBizFromVO> paramlist,List<HeadEntityVO> headEntityVOList) {
        List<QuestionnaireSqlContentVO> returnQuestionnairelist=new ArrayList<>();
        QuestionnaireSqlContentVO questionnaireSqlContentVO =getNewQuestionnaireContent();
        questionnaireSqlContentVO.setFiled(req.dataIndex);
        HeadEntityVO headEntityVO=new HeadEntityVO();
        headEntityVO.setBindname(req.dataIndex);
        headEntityVO.setHeadname(req.title);
        headEntityVOList.add(headEntityVO);
        ParamBizFromVO item=new ParamBizFromVO();
        switch (req.type) {
            case "input":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " like CONCAT('%',#{" + req.dataIndex + "},'%') </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "textarea":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` text   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " like CONCAT('%',#{" + req.dataIndex + "},'%') </if>");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "select":
                if(req.options.getIsMulty()!=null&&req.options.getIsMulty().equals(true)){
                    questionnaireSqlContentVO.getTableEidtSql().append("`"+req.dataIndex+"` varchar(500)   DEFAULT NULL ");
                    questionnaireSqlContentVO.setColumnType("varchar(500)");
                    questionnaireSqlContentVO.getSelectListSql().append("<if test=\""+req.dataIndex+"!=null and "+req.dataIndex+"!=''\">and "+req.dataIndex+"like CONCAT('%',#{"+req.dataIndex+"},'%') </if>");
                }else {
                    questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                    questionnaireSqlContentVO.setColumnType("varchar(50)");
                    questionnaireSqlContentVO.getSelectListSql().append("<if test=\""+req.dataIndex+"!=null and "+req.dataIndex+"!=''\">and "+req.dataIndex+" = #{"+req.dataIndex+"} </if>");
                }
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(req.options.getIsMulty(),req.options.filed,req.options.getOptions()));
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "date":
                switch (req.options.modeltype) {
                    case "date":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + ".size()>0\">and " + req.dataIndex + " between #{" + req.dataIndex + "[0]} and #{" + req.dataIndex + "[1]}  </if>");
                        break;
                    case "year":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and date_format(" + req.dataIndex + ",'%Y') = date_format(#{" + req.dataIndex + "},'%Y') </if>");
                        break;
                    case "month":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and date_format(" + req.dataIndex + ",'%Y%m') = date_format(#{" + req.dataIndex + "},'%Y%m') </if>");
                        break;
                    case "week":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "`  varchar(50)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + ".size()>0\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                        break;
                    case "range":
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "`  varchar(50)   DEFAULT NULL ");
                        // selectListSql.append("<if test=\"" + req.options.filed + "!=null and " + req.options.filed + "!=''\">and date_format(" + req.options.filed + ",'%Y') = date_format(#{" + req.options.filed + "},'%Y') </if>");
                        break;
                    default:
                        questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` datetime(0)   DEFAULT NULL ");
                        questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                        break;

                }
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                break;
            case "radio":
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(false,req.dataIndex,req.options.getOptions()));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "checkbox":
                questionnaireSqlContentVO.getChartQuerySql().append(chartFiledSql(true,req.dataIndex,req.options.getOptions()));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " like CONCAT('%',#{" + req.dataIndex + "},'%') </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                if(req.options.isRemote) {
                    item.setFormId(querybyTableName(req.options.sourceApi));
                }
                paramlist.add(item);
                break;
            case "switch":
                questionnaireSqlContentVO.getChartQuerySql().append(chartswitchFiledSql(req.dataIndex));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` tinyint(1)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "==true?1:0}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "#{" + tableName + "[$index0]." + req.dataIndex + "==true?1:0}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "slider":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.dataIndex));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + ".size()>0\">and " + req.dataIndex + " between  #{" + req.dataIndex + "[0]} and #{" + req.dataIndex + "[1]} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "colorpicker":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "editor":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " like CONCAT('%',#{" + req.dataIndex + "},'%') </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` text   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "rate":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.dataIndex));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "inputnumber":
                questionnaireSqlContentVO.getChartQuerySql().append(chartNumberFiledSql(req.dataIndex));
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` int(16)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\">and " + req.dataIndex + " = #{" + req.dataIndex + "} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "timepicker":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + ".size()>0\">and " + req.dataIndex + " between #{" + req.dataIndex + "[0]}  and #{" + req.dataIndex + "[1]} </if>");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case "upFilesComp":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(500)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." + req.dataIndex + "}");
                break;
            case"user":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.dataIndex + " = #{" + req.dataIndex + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." +req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{" + tableName + "[$index0]." +req.dataIndex+ "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"organ":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.dataIndex + " = #{" + req.dataIndex + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{" + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." + req.dataIndex+ "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"department":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.dataIndex + " = #{" + req.dataIndex + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." +req.dataIndex+ "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"role":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.dataIndex + " = #{" + req.dataIndex + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." +req.dataIndex+ "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"imgage":
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(50)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." + req.dataIndex+ "}");
                break;
            case"cityselector":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(150)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            case"datarelevance":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.options.filed + " = #{" + req.options.filed + "}  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(150)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." + req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                item.setFormId(querybyTableName( req.options.sourceApi));
                paramlist.add(item);
                returnQuestionnairelist.add(getNewQuestionnaireContent());
                returnQuestionnairelist.get(0).getSelectListSql().append("<if test=\"" + req.dataIndex + "_text!=null and " + req.dataIndex + "_text!=''\" >and " + req.options.filed + "_text = #{" + req.options.filed + "_text}  </if>");
                returnQuestionnairelist.get(0).getTableEidtSql().append("`" + req.dataIndex + "_text` varchar(150)   DEFAULT NULL ");
                returnQuestionnairelist.get(0).getInsertFiledSql().append(",#{"  + tableName + "[$index0]." + req.dataIndex + "_text}");
                returnQuestionnairelist.get(0).getUpdateFiledSql().append(" ," + req.dataIndex + "_text=#{"  + tableName + "[$index0]." + req.dataIndex + "_text}");
                returnQuestionnairelist.get(0).setFiled( req.dataIndex + "_text");
                headEntityVO.setBindname(req.dataIndex + "_text");
                headEntityVO.setHeadname(req.title);
                headEntityVO.setFromId(item.getFormId());
                //                ParamBizFromVO item2=new ParamBizFromVO();
//                item2.setVariabletype("常量");
//                item2.setRequired(false);
//                item2.setProperty(req.dataIndex);
//                item2.setParamname(req.title);
//                item2.setFormId(querybyTableName( req.options.sourceApi));
//                paramlist.add(item2);
                break;
            case"fromlable":
                questionnaireSqlContentVO.getSelectListSql().append("<if test=\"" + req.dataIndex + "!=null and " + req.dataIndex + "!=''\" >and " + req.options.filed + " like CONCAT('%', #{" + req.options.filed + "},'%')  </if>");
                questionnaireSqlContentVO.getTableEidtSql().append("`" + req.dataIndex + "` varchar(150)   DEFAULT NULL ");
                questionnaireSqlContentVO.getInsertFiledSql().append(",#{"  + tableName + "[$index0]." +req.dataIndex + "}");
                questionnaireSqlContentVO.getUpdateFiledSql().append(" ," + req.dataIndex + "=#{"  + tableName + "[$index0]." +req.dataIndex + "}");
                item.setVariabletype("常量");
                item.setRequired(false);
                item.setProperty(req.dataIndex);
                item.setParamname(req.title);
                paramlist.add(item);
                break;
            default:
                questionnaireSqlContentVO = null;
                break;
        }
        if (questionnaireSqlContentVO != null) {
            questionnaireSqlContentVO.getTableEidtSql().append("COMMENT '" + req.title.replace("'", "''") + "' ");
        }
        returnQuestionnairelist.add(questionnaireSqlContentVO);
        return returnQuestionnairelist;
    }
    ///根据数据源Id 查找对应表单信息
    String querybyTableName(String sourceApi){
        String tableNameStr=   sourceApi.substring(0, sourceApi.indexOf("."));
        if(StringUtils.isNotBlank(tableNameStr)){
            TappFromEntity fromEntity= tappFromService.querybyTableName(tableNameStr);
            if(fromEntity!=null){
                return fromEntity.getFid().toString();
            }
        }
        return "";
    }

}
